package com.yqg.protection.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.protection.common.Result;
import com.yqg.protection.entity.OldClient;
import com.yqg.protection.mapper.OldClientMapper;
import com.yqg.protection.service.IOldClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * OldClientController 前端控制器
 *
 * @author zxf
 * @since 2024-08-03
 */
@RestController
@RequestMapping("/old-client")
public class OldClientController {

    @Resource
    private IOldClientService oldClientService;

    @Autowired
    private OldClientMapper oldClientMapper;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    public Result save(@RequestBody OldClient oldClient) {
        oldClient.setIsDelete(0);
        return Result.success(oldClientService.saveOrUpdate(oldClient));
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        OldClient oldClient = oldClientService.getById(id);
        return Result.success(oldClientService.saveOrUpdate(oldClient));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<OldClient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        List<OldClient> list = oldClientService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(oldClientService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String address) {
        QueryWrapper<OldClient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.like("address", address);
        queryWrapper.orderByDesc("id");
        return Result.success(oldClientService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    /**
     * 查询所有
     */
    @GetMapping("/allUrlName")
    public Result findAllUrlName() {
        List<OldClient> list = oldClientMapper.findAllUrlName();
        return Result.success(list);
    }

}

